﻿using B.S.Production.Domain.Entities;
using B.S.Production.ErrorCode;
using B.S.Production.Write.API.Application.Command.ProductionOrderCommand;
using MediatR;
using SqlSugar;

namespace B.S.Production.Write.API.Application.CommandHandlers.ProductionOrderCommandHandler
{
    public class DeleteProductionOrderCommandHandler : IRequestHandler<DeleteProductionOrderCommand, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;

        public DeleteProductionOrderCommandHandler(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(DeleteProductionOrderCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();    
            var list = await db.Queryable<ProductionOrder>().FirstAsync(x=>x.ProdunctionId == request.ProdunctionId);
            list.IsDelete = true;
            result.Data = await db.Updateable<ProductionOrder>(list).ExecuteCommandAsync();
            if (result.Data > 0)
            {
                result.Code = ApiEnums.删除成功;
                result.Msg = ApiEnums.删除成功.ToString();
            }
            else
            {
                result.Code = ApiEnums.删除失败;
                result.Msg = ApiEnums.删除失败.ToString();
            }
            return result;
        }
    }
}
